<?php
/**
 * Created by PhpStorm.
 * User: jz
 * Date: 16/7/7
 * Time: 下午1:58
 */

function register($email, $username, $password)
{
    //register new user with db
    //return ture or error message
    
    $conn = db_connect();
    $res = $conn->query(sprintf('select * from user where username = \'%s\'', $username));
    if (!$res){
        throw new Exception('不能执行查询');
    }
    if ($res->num_rows > 0){
        throw new Exception('该用户已存在,请使用其他名字');
    }
    $res = $conn->query(sprintf('insert into user values(\'%s\', sha1(\'%s\'), \'%s\')', $username, $password, $email));
    if (!$res){
        throw new Exception('无法注册,请重试');
    }
    return true;
}

function login($username, $password)
{
    $conn = db_connect();
    $res = $conn->query(sprintf('select * from user where username = \'%s\' and passwd = sha1(\'%s\')', $username, $password));
    if (!$res){
        throw new Exception('无法登陆');
    }
    if ($res->num_rows > 0){
        return true;
    }
    throw new Exception('用户名错或密码错');
}

function check_valid_user()
{
    if (isset($_SESSION['valid_user'])){
        echo "登陆用户:" . $_SESSION['valid_user'] . "<br>";
    }else {
        do_html_heading('出错');
        echo '您还没有登陆';
        do_html_url('login.php', '登陆');
        do_html_footer();
        exit();
    }
    
}

function change_password($user, $old, $new)
{
    login($user, $old);
    $conn = db_connect();
    $res = $conn->query(
            sprintf(
                'update user set passwd = sha1(\'%s\')', $new
            )
        );
    
    
}

function get_random_word($min, $max)
{
    $dic = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $length = rand($min, $max);
    $word = '';
    for ($i = 0; $i < $length; $i++){
       $word .= $dic[rand(0, 51)]; 
    }
    return $word;
}

function reset_passwd($user)
{
    $new_passwd = get_random_word(6,13);
    $new_passwd .= rand(0,999);
    $conn = db_connect();
    $str = sprintf('update user set passwd = sha1(\'%s\') where username = \'%s\'', $new_passwd, $user);
//    echo $str;
    $res = $conn->query($str);
    if (!$res){
        throw new Exception('无此用户');
    }else {
        return $new_passwd;
    }
    
}

function notify_passwd($user, $passwd)
{
    require_once ('mymail.php');
    $conn = db_connect();
    $res = $conn->query(
        sprintf('
            select * from user where username = \'%s\'
        ', $user
        )
    );
    if (!$res){
        throw new Exception('无此用户');
    }
    if ($res->num_rows == 0){
        throw new Exception('无此用户');
    }
    $row = $res->fetch_object();
//    $email = $row->email;
    $email = "467847281@qq.com";
    $msg = "您的密码被重置为$passwd";

    $mail = new MySendMail();
    $mail->setServer('smtp.163.com', 'jinzhao115@163.com', 'jinzhao1988');
    $mail->setFrom('jinzhao115@163.com', 'phpbookmark');
    $mail->setReceiver($email);
    $mail->setMailInfo('phpbookmark 登陆信息', $msg);
    if ($mail->sendMail()){
        return true;
    }
    throw new Exception('发送邮件失败');
}